package sw.EngAdroid;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

//The game class contains the running methods to actually run the game
//It creates a PlayerClass Obj to store player information and keep
//track of players and their stats

public class Game {

	// Random comment to see if committing works...

	Scanner keyboard = new Scanner(System.in); // Game set up variables

	private String name;
	private ArrayList<Player> players = new ArrayList<Player>(); // Array list
																	// of the
																	// players
																	// in the
																	// game
	private List<Question> questions = new ArrayList<Question>(); // ArrayList
																		// of
																		// questions
																		// for
																		// the
																		// game
	private int num_players; // number of players in the game, for multiplayer
								// set up?
	private String score_type;
	/*
	 * how are we going to do scoring? how are we going to end the game? 1) 7
	 * turns, max score 2) score limit
	 */
	private Question current_question; // Current question
	private int nextQuestion = 0; // Location in the question ArrayList
	private ArrayList<Guess> guesses = new ArrayList<Guess>(); // ArrayList of
																// the guesses
																// for the
																// current
																// question
	private ArrayList<Bet> bets = new ArrayList<Bet>(); // ArrayList of the bets
														// for the current
														// question
	private boolean running = false; // Checks whether the game is running or
										// not.
	// Unique identifier to distinguish network games.
	private int gameID;
	// State of the network game this object represents.
	private int gameState;
	
	/*
	 * Default constructor
	 */
	public Game() {
		current_question = new Question();
		//getQuestionsFromFile("questions.txt");
	}

	/*
	 * Constructor
	 */
	public Game(String game_name, int players) {
		name = game_name;
		num_players = players;
		current_question = new Question();
		//getQuestionsFromFile("questions.txt");
		/*
		 * for(int i = 0; i < 10; i++){ questions.add(new Question()); }
		 */
	}
	
	public Game(String gameName, int players, int gameState){
		name = gameName;
		num_players = players;
		current_question = new Question();
		this.gameState = gameState;
	}

	/*
	 * Getter for name variable
	 */
	public String getName() {
		return name;
	}

	/*
	 * Setter for name variable
	 */
	public void setName(String n) {
		name = n;
	}

	/*
	 * Adds players to the game after checking if the game is full
	 */
	public boolean addPlayer(Player p) {
		if (num_players > players.size() && !running) {
			players.add(p);
			return true;
		} else
			return false;
	}

	/*
	 * Removes a player from the game after checking if he was in the game
	 */
	public boolean removePlayer(Player p) {
		if (players.contains(p) && !running) {
			players.remove(p);
			return true;
		} else {
			return false;
		}

	}

	/*
	 * Updates the current question and displays it Currently does a print line
	 */
	public void displayQuestion() {
		current_question = questions.get(nextQuestion);
		System.out.println(current_question.getText());
	}

	/*
	 * Displays the answer for the current question, currently as a print line
	 * Increments nextQuestion and updates current_question
	 */
	public void displayAnswer() {
		System.out.println("The answer is: " + current_question.getAnswer());
		nextQuestion++;
		current_question = questions.get(nextQuestion);
		guesses = new ArrayList<Guess>();
		bets = new ArrayList<Bet>();
	}

	/*
	 * Used to add a guess to the guesses ArrayList
	 */
	public void receiveGuess(Guess guess) {
		guesses.add(guess);
	}

	/*
	 * Used to add a bet to the bets ArrayList
	 */
	public void receiveBet(Bet bet) {
		bets.add(bet);
	}

	/*
	 * Prints out the bets received Useful for testing/debugging.
	 */
	public void getBets() {
		for (int i = 0; i < bets.size(); i++) {
			System.out.println(bets.get(i).getName() + "  "
					+ bets.get(i).getChoice() + "   "
					+ bets.get(i).getPermaChips() + "  "
					+ bets.get(i).getGameChips());
		}
	}

	/*
	 * returns the list of bets
	 */
	public ArrayList<Bet> getBetList() {
		return bets;
	}

	/*
	 * returns running boolean
	 */
	public boolean getRunning() {
		return running;
	}

	/*
	 * sets the running boolean
	 */
	public void setRunning(boolean selection) {
		running = selection;
	}

	/*
	 * Calculates results from bets To be used after all bets are in
	 */
	public void winnings() {
		int answer = current_question.getAnswer();
		Guess winner = guesses.get(0);
		int win_location = 0;
		// The for loop below determines which guess was closest to the answer
		for (int i = 0; i < guesses.size(); i++) {
			int difference = Math.abs(answer - guesses.get(i).getGuess());
			if (difference < (Math.abs(answer - winner.getGuess()))) {
				winner = guesses.get(i);
				win_location = i;
			}
		}
		// The for loop below gives chips to whoever bet on the winning guess
		// Currently it just gives them a 2x return on their bet
		// Doesn't currently remove chips for bets on wrong guesses
		for (int j = 0; j < bets.size(); j++) {
			if (bets.get(j).getChoice() == win_location) {
				String name = bets.get(j).getName();
				for (int z = 0; z < players.size(); z++) {
					if (name.equals(players.get(z).getPlayer_name())) {
						players.get(z).setGame_chips(
								bets.get(j).getPermaChips()
										+ players.get(z).getGame_chips());
					}
				}
			}
		}
	}

	
	public List<Guess> getGuesses() {		
		return guesses;
	}
	
	/*
	 * Prints out the guesses Useful for testing/debugging.
	 */
	public void displayGuesses(){
		for (int i = 0; i < guesses.size(); i++) {
			System.out.println(i + "  " + guesses.get(i).getName() + "     "
					+ guesses.get(i).getGuess());
		}
	}

	public Question getQuestion() {
		return current_question;
	}

	public List<Question> getQuestions() {
		return questions;
	}

	public ArrayList<Player> getPlayers() {
		return players;
	}

	private void getQuestionsFromFile(String fileName) {
		//List<Question> questions = new ArrayList<Question>();
		try {
			Scanner in = new Scanner(new File(fileName));
			while (in.hasNextLine()) {
				String question = in.nextLine();
				int answer = Integer.parseInt(in.nextLine());
				in.nextLine();
				questions.add(new Question(question, answer));
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		Collections.shuffle(questions);
		//return questions;
	}

	public void setGameID(int gameID) {
		this.gameID = gameID;
	}

	public int getGameID() {
		return gameID;
	}
	
	/*
	 * Setter for players
	 * Created By Nicole for use in Networking Code
	 */
	
	public void setPlayers(ArrayList<Player> p){
		players = p;
	}

	public int getGameState() {
		return gameState;
	}

	public void setGameState(int gameState) {
		this.gameState = gameState;
	}
	
	public void resetChipBets() {
        for (int i = 0; i < players.size(); i++) {
                players.get(i).setChipsBet(0);
        }
	}
	
	@Override
	public String toString()
	{
		return name;
	}
}